package com.servlet.admin;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.entity.Menu;
import com.entity.User;
import com.service.menurole.MenuServiceImpl;
import com.service.product.impl.OrderServiceImpl;
import com.service.user.impl.UserServiceImpl;
import com.servlet.BaseServlet;
import com.utils.EmptyUtils;
import com.utils.Pager;
import com.utils.ReturnResult;

@WebServlet("/AdminUserServlet")
public class AdminUserServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	/**
	 * 用户详情！
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public String index(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 封装成对象！
		User easybuyUser = new User();
		request.setAttribute("user", easybuyUser);
		request.setAttribute("menu", 2);
		// 调用三层！
		return "backend/user/Member_User";
	}
	
	/**
	 * 加载用户列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public String user(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 获取当前页数！
		String currentPageStr = request.getParameter("currentPage");
		// 获取页大小！
		String pageSize = request.getParameter("pageSize");
		int rowPerPage = EmptyUtils.isEmpty(pageSize) ? 10 : Integer.parseInt(pageSize);
		int currentPage = EmptyUtils.isEmpty(currentPageStr) ? 1 : Integer.parseInt(currentPageStr);
		// 获取总页数！
		int count = new UserServiceImpl().finUserCount();
		Pager pager = new Pager(count, rowPerPage, currentPage);
		if (pager.getPageCount() < pager.getCurrentPage()) {
			pager.setCurrentPage(currentPage - 1);
		}
		pager.setUrl("/EasybuyUserServlet?action=user");
		request.setAttribute("pager", pager);
		request.setAttribute("menu", 8);
		// 调用三层！
		List<User> list = new UserServiceImpl().finUserAll(pager);
		request.setAttribute("list", list);
		User user = (User) request.getSession().getAttribute("easybuyUserLogin");
		//查询用户按钮
		int mfatherid = Integer.parseInt(request.getParameter("mfatherid"));
		List<Menu> menuListBtn = new MenuServiceImpl().menuBtn(user.getId(),mfatherid);
		request.setAttribute("menuListBtn", menuListBtn);
		return "backend/user/Member_Packet";
	}
	
	/**
	 * 点击新增用户！
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public String add(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 封装成对象！
		User easybuyUser = new User();
		request.setAttribute("user", easybuyUser);
		// 调用三层！
		return "backend/user/toUpdateUser";
	}
	/**
	 * 传递参数到修改信息窗口
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public String toUpdateUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 获取用户ID！
		String id = request.getParameter("id");
		String currentPage = request.getParameter("currentPage");
		// 调用三层！
		User user = new UserServiceImpl().finUserById(Integer.parseInt(id));
		request.setAttribute("user", user);
		request.setAttribute("currentPage", currentPage);
		return "/backend/user/toUpdateUser";
	}
	
	/**
	 * 删除用户
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ReturnResult del(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ReturnResult result = new ReturnResult();
		// 获取要删除的用户ID！
		int id = Integer.parseInt(request.getParameter("id"));
		// **************
		// 执行删除的时候先进去判断该用户是否有订单信息！
		int find = new OrderServiceImpl().finuserByIdOrder(id);
		// 执行删除的时候先进去判断该用户是否有地址信息！
		int count = new UserServiceImpl().finUserByIdAddress(id);
		//执行删除查看是否是管理员
		User user = new UserServiceImpl().finUserById(id);
		// 声明变量接受结果！
		int del = -1;
		// 判断外键是否有数据！
		if (find == 0) {
			if (count == 0) {
				//判断是否是管理员
				if(user.getType() == 1) {
					result.returnNo("该用户是管理员无法删除！");
				}else {
					// 调用三层！
					del = new UserServiceImpl().finRemoveUserById(id);
					// 判断是否删除成功！
					if (del > 0) {
						result.returnYes("删除成功");
					}
				}
			} else {
				// 删除失败！！
				result.returnNo("该用户下有地址信息，无法删除！");
			}
		} else {
			// 删除失败！！
			result.returnNo("该用户下有订单信息，无法删除！");
		}
		return result;
		// *************
	}
	
	/**
	 * 修改用户信息/添加用户信息！
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ReturnResult modify(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 实例化向页面输出对象！
		ReturnResult result = new ReturnResult();
		// 封装成对象！
		User easybuyUser = new User();
		// 获取用户需要更新的数据！
		easybuyUser.setId(
				EmptyUtils.isEmpty(request.getParameter("id")) ? 0 : Integer.parseInt(request.getParameter("id")));

		easybuyUser.setLoginName(request.getParameter("name"));
		if (easybuyUser.getId() == 0) {
			int count=new UserServiceImpl().finLoginNameCount(easybuyUser.getLoginName());
			if(count==1) {
				 result.returnNo("用户名已存在");
			}else {
				easybuyUser.setUserName(request.getParameter("userName"));
				easybuyUser.setPassword(request.getParameter("password"));
				easybuyUser.setSex(Integer.parseInt(request.getParameter("sex")));
				easybuyUser.setIdentityCode(request.getParameter("identityCode"));
				easybuyUser.setEmail(request.getParameter("email"));
				easybuyUser.setMobile(request.getParameter("mobile"));
				easybuyUser.setType(Integer.parseInt(request.getParameter("user")));
				// 调用三层！
				int modify = new UserServiceImpl().finModifyUserId(easybuyUser);		
				// 判断是否删除成功！
				if (modify > 0) {
					result.returnYes("操作成功");
				} else {
					result.returnNo("操作失败！请联系管理员。。");
				}
			}
		}else {
			easybuyUser.setUserName(request.getParameter("userName"));
			easybuyUser.setPassword(request.getParameter("password"));
			easybuyUser.setSex(Integer.parseInt(request.getParameter("sex")));
			easybuyUser.setIdentityCode(request.getParameter("identityCode"));
			easybuyUser.setEmail(request.getParameter("email"));
			easybuyUser.setMobile(request.getParameter("mobile"));
			easybuyUser.setType(Integer.parseInt(request.getParameter("user")));
			// 调用三层！
			int modify = new UserServiceImpl().finModifyUserId(easybuyUser);		
			// 判断是否删除成功！
			if (modify > 0) {
				result.returnYes("操作成功");
			} else {
				result.returnNo("操作失败！请联系管理员。。");
			}
		}
		
		return result;
	}

	@Override
	public Class<?> getServletClass() {
		// TODO Auto-generated method stub
		return AdminUserServlet.class;
	}

}
